/**
 * https://github.com/google/web-starter-kit
 */
'use strict';

import path from 'path';
import gulp from 'gulp';
import del from 'del';
import runSequence from 'run-sequence';
import browserSync from 'browser-sync';
import swPrecache from 'sw-precache';
import gulpLoadPlugins from 'gulp-load-plugins';
import {output as pagespeed} from 'psi';
import pkg from './package.json';
import gulpCopy from 'gulp-file-copy';
import sass from 'gulp-sass';
import fileInclude  from 'gulp-file-include';



const $ = gulpLoadPlugins();
const reload = browserSync.reload;

//path
const appDir = `${__dirname}/app`;
const dirVars={
	vendor:`${appDir}/vendor`,
	common:`${appDir}/common`,
	themes:`${appDir}/themes`


}



gulp.task('lint',cb=>{
	gulp.src(['app/scripts/**/*.js','!node_modules/**'])
    .pipe($.eslint())
    .pipe($.eslint.format())
    .pipe($.if(!browserSync.active, $.eslint.failAfterError()))

});


//第三方库
gulp.task('clean:vendor',()=>del(['dist/lib/*','!dist/lib/wechat-card'],{dot:true}));
gulp.task('vendor',['clean:vendor'],cb=>{
	//gulp.src(['./app/vendor/**/*.js']).pipe(gulpCopy('./dist/lib/mui',{start:'./app/vendor/mui/dist'}));
	gulp.src([`${dirVars.vendor}/mui/dist/**`],{base:`${dirVars.vendor}/mui/dist`,dot:true}).pipe(gulp.dest('dist/lib/mui/'));
	gulp.src(`${dirVars.vendor}/jquery.?(*.)js`).pipe(gulp.dest('dist/lib'));
	gulp.src(`${dirVars.vendor}/gsap/*`).pipe(gulp.dest('dist/lib'));
	cb();
});



//公用部分
//fonts
gulp.task('common:fonts',cp=>{
	gulp.src([`${dirVars.common}/fonts/*.ttf`],{base:dirVars.common, dot:true})
		.pipe($.newer('.tmp/wechat-card'))
		.pipe(gulp.dest('.tmp/wechat-card'))
	    .pipe(gulp.dest(`dist/lib/wechat-card/`));

});

//svg
gulp.task('common:svg',cb=>{
	console.log($.svgSymbols);
    gulp.src([`${dirVars.common}/svg/element/*.svg`])
    .pipe($.svgSymbols())
    .pipe(gulp.dest('dist/lib/wechat-card/svg'));
});

//scss css
gulp.task('common:style',()=>{
	const AUTOPREFIXER_BROWSERS = [
	    'ie >= 10',
	    'ie_mob >= 10',
	    'ff >= 30',
	    'chrome >= 34',
	    'safari >= 7',
	    'opera >= 23',
	    'ios >= 7',
	    'android >= 4.4',
	    'bb >= 10'
	];

	

	gulp.src([`${dirVars.common}/scss/wechat-card.scss`])
        .pipe($.newer('.tmp/wechat-card/css'))
        .pipe($.sourcemaps.init())
        .pipe($.sass({
	      precision: 10
	    }).on('error', $.sass.logError))
	    .pipe($.autoprefixer(AUTOPREFIXER_BROWSERS))
	    .pipe(gulp.dest('.tmp/wechat-card/css'))
	    .pipe($.if('*.css', $.cssnano()))
	    .pipe($.size({title: 'styles'}))
	    .pipe($.sourcemaps.write('./'))
	    .pipe(gulp.dest('dist/lib/wechat-card/css'))
	    .pipe(gulp.dest('.tmp/wechat-card/css'));
});

gulp.task('lib',['vendor','common:style','common:svg','common:fonts'],cp=>{
    console.log('Common module is completed!');
});


// default theme

gulp.task('defaultTheme:style',()=>{
   gulp.src([`${dirVars.themes}/**/*.scss`,`!**/_*.scss`],{dot:true})
   .pipe($.sourcemaps.init())
   .pipe($.sass({
   	  precision: 10
   }).on('error',$.sass.logError))
   .pipe($.rename({
   	 dirname:'default/css'
   }))
   .pipe($.sourcemaps.write('./'))
   .pipe(gulp.dest('dist/themes'));
});

gulp.task('defaultTheme:image',()=>{
   gulp.src([`${dirVars.themes}/**/*.png`,`${dirVars.themes}/**/*.jpg`,`${dirVars.themes}/**/*.jepg`,`${dirVars.themes}/**/*.svg`],{dot:true})
   .pipe(gulp.dest('dist/themes'));
});

gulp.task('defaultTheme:scripts',()=>{
	gulp.src([`${dirVars.themes}/default/scripts/main.js`])
	.pipe($.newer('.tmp/themes/default/scripts'))
	.pipe($.sourcemaps.init())
	.pipe($.babel({
		presets: ['es2015']
	}))
	.pipe($.sourcemaps.write())
	.pipe(gulp.dest('.tmp/themes/default/scripts'))
	.pipe($.concat('main.min.js'))
	.pipe($.uglify({preserveComments:'some'}))
	//Output files
	.pipe($.size({title:'scripts'}))
	.pipe($.sourcemaps.write('.'))
	.pipe(gulp.dest('dist/themes/default/scripts'))
	.pipe(gulp.dest('.tmp/themes/default/scripts'));
});


gulp.task('defaultTheme',['defaultTheme:style','defaultTheme:image','defaultTheme:scripts'],cb=>{
	console.log('default theme is completed!');
});




gulp.task('build-page',cb=>{
	
	gulp.src([`${dirVars.themes}/**/*.html`,`${dirVars.themes}/*.html`,`!**/_*.html`],{dot:true})
		.pipe($.newer('.tmp/themes'))
	    .pipe(fileInclude({
	    	prefix: '@@',
            basepath: '@file'
	    }))
	    .pipe(gulp.dest('dist/themes'))
	    .pipe(gulp.dest('.tmp/theme'));
	

});


gulp.task('default',cb=>{
	//runSequence('lint', 'generate-service-worker',cb);
	gulp.run(['lib','build-page','defaultTheme']);
});



// Watch files for changes & reload
gulp.task('server', () => {
	
	browserSync({
    notify: false,
    // Customize the Browsersync console logging prefix
    logPrefix: 'WSK',
    // Allow scroll syncing across breakpoints
    scrollElementMapping: ['main', '.mdl-layout'],
    // Run as an https by uncommenting 'https: true'
    // Note: this uses an unsigned certificate which on first access
    //       will present a certificate warning in the browser.
    // https: true,
    server: 'dist',
    port: 3000
  });


	gulp.watch(['app/themes/**/*.html','app/themes/**/**/*.html'], reload);
    //gulp.watch(['app/styles/**/*.{scss,css}'], ['styles', reload]);
    //gulp.watch(['app/scripts/**/*.js'], ['lint', 'scripts', reload]);
    //gulp.watch(['app/images/**/*'], reload);


 });








